/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseWorkReportDao.cs
*        创建系统时间:       2012/11/2 19:07:00
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System;
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseWorkReportDao : BaseDao
    {
        public BaseWorkReportDao()
        {
            base.CurrentTableName = BaseWorkReportTable.TableName;
        }

        public BaseWorkReportDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseWorkReportDao(BaseUserInfo userInfo)
            : this()
        {
            base.UserInfo = userInfo;
        }

        public BaseWorkReportDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string Add(BaseWorkReportEntity workReportEntity, out string statusCode)
        {
            string str = string.Empty;
            str = this.AddEntity(workReportEntity);
            statusCode = StatusCode.OKAdd.ToString();
            return str;
        }

        private string AddEntity(BaseWorkReportEntity workReportEntity)
        {
            string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseWorkReportTable.TableName);
            workReportEntity.SortCode = sequence;
            BaseStaffDao dao = new BaseStaffDao(base.DbHelper, base.UserInfo);
            string property = dao.GetProperty(workReportEntity.StaffID, BaseStaffTable.FieldCompanyID);
            string targetValue = dao.GetProperty(workReportEntity.StaffID, BaseStaffTable.FieldDepartmentID);
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginInsert(BaseWorkReportTable.TableName);
            sqlBuilder.SetValue(BaseWorkReportTable.FieldID, sequence);
            sqlBuilder.SetValue(BaseWorkReportTable.FieldCompanyID, property);
            sqlBuilder.SetValue(BaseWorkReportTable.FieldDepartmentID, targetValue);
            this.SetEntity(sqlBuilder, workReportEntity);
            sqlBuilder.SetValue(BaseWorkReportTable.FieldCreateUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseWorkReportTable.FieldCreateDate);
            return ((sqlBuilder.EndInsert() > 0) ? sequence : string.Empty);
        }

        public int BatchSave(DataTable dataTable)
        {
            int num = 0;
            BaseWorkReportEntity workReportEntity = new BaseWorkReportEntity();
            foreach (DataRow row in dataTable.Rows)
            {
                if (row.RowState == DataRowState.Modified)
                {
                    workReportEntity.GetFrom(row);
                    num += (this.UpdateEntity(workReportEntity) > 0) ? 1 : 0;
                }
                if (row.RowState != DataRowState.Unchanged)
                {
                    DataRowState rowState = row.RowState;
                }
            }
            return num;
        }

        public DateTime[] CheckWorkDate(string paramstaffID)
        {
            DateTime[] timeArray = new DateTime[0x1f];
            for (int i = -30; i <= 0; i++)
            {
                DateTime date = DateTime.Now.AddDays((double)i);
                if (!this.ExistDate(date, paramstaffID))
                {
                    timeArray[30 + i] = date;
                }
            }
            return timeArray;
        }

        private bool ExistDate(DateTime date, string staffID)
        {
            bool flag = false;
            string sqlQuery = " SELECT " + BaseWorkReportTable.FieldWorkDate + " FROM " + BaseWorkReportTable.TableName + " WHERE " + BaseWorkReportTable.FieldWorkDate + " = '" + date.ToShortDateString() + "' AND " + BaseWorkReportTable.FieldStaffID + "='" + staffID + "'";
            if (base.DbHelper.ExecuteNonQuery(sqlQuery) != 0)
            {
                flag = true;
            }
            return flag;
        }

        public DataTable GetListByUser(string staffID, string date)
        {
            string str4;
            string sqlQuery = " SELECT A.*, B." + BaseStaffTable.FieldRealname + " AS StaffFullName    FROM " + BaseWorkReportTable.TableName + " AS A LEFT JOIN " + BaseStaffTable.TableName + " AS B ON B." + BaseStaffTable.FieldID + "=A." + BaseWorkReportTable.FieldStaffID;
            DataTable dataTable = new DataTable(BaseWorkReportTable.TableName);
            if (staffID.Trim().Length != 0)
            {
                string str2 = sqlQuery;
                sqlQuery = str2 + " WHERE A." + BaseWorkReportTable.FieldStaffID + " = '" + staffID + "'";
            }
            if (date.Trim().Length == 0)
            {
                switch (BaseConfiguration.Instance.DataBaseType)
                {
                    case DataBaseType.Oracle:
                    case DataBaseType.Bd2:
                        goto Label_0171;

                    case DataBaseType.Sqlserver:
                    case DataBaseType.Access:
                        sqlQuery = sqlQuery + " AND DATEPART(WEEK, " + BaseWorkReportTable.FieldWorkDate + ") = DATEPART(WEEK, GETDATE())";
                        goto Label_0171;

                    case DataBaseType.MySql:
                        sqlQuery = sqlQuery + " AND WEEK(" + BaseWorkReportTable.FieldWorkDate + ") = WEEK(now())";
                        goto Label_0171;
                }
            }
            else if (date.Trim().Length > 0)
            {
                string str3 = sqlQuery;
                sqlQuery = str3 + " AND " + BaseWorkReportTable.FieldWorkDate + " = '" + date + "'";
            }
        Label_0171:
            str4 = sqlQuery;
            sqlQuery = str4 + " ORDER BY A." + BaseWorkReportTable.FieldEnabled + ", A." + BaseWorkReportTable.FieldWorkDate + " DESC ";
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public DataTable Search(int enabled, DateTime startDate, DateTime endDate)
        {
            string sqlQuery = " SELECT A.*, B." + BaseStaffTable.FieldRealname + " AS StaffFullName  ,C." + BaseStaffTable.FieldUsername + " AS AuditName    FROM " + BaseWorkReportTable.TableName + " AS A LEFT JOIN " + BaseStaffTable.TableName + " AS B ON B." + BaseStaffTable.FieldID + "=A." + BaseWorkReportTable.FieldStaffID + " LEFT JOIN " + BaseStaffTable.TableName + " AS C ON C." + BaseStaffTable.FieldID + "=A." + BaseWorkReportTable.FieldAuditStaffID;
            DataTable dataTable = new DataTable(BaseWorkReportTable.TableName);
            object obj2 = sqlQuery;
            sqlQuery = string.Concat(new object[] { obj2, " WHERE A.", BaseWorkReportTable.FieldEnabled, " = ", enabled });
            if (startDate.ToString().Trim().Length > 0)
            {
                object obj3 = sqlQuery;
                sqlQuery = string.Concat(new object[] { obj3, " AND A.", BaseWorkReportTable.FieldWorkDate, " >= '", startDate, "'" });
            }
            if (endDate.ToString().Trim().Length > 0)
            {
                object obj4 = sqlQuery;
                sqlQuery = string.Concat(new object[] { obj4, " AND A.", BaseWorkReportTable.FieldWorkDate, " <= '", endDate, "'" });
            }
            if (!base.UserInfo.IsAdministrator)
            {
                BaseResourcePermissionScopeDao dao = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo);
                string str2 = BaseBusinessLogic.ObjectsToList(dao.GetUserIDs(base.UserInfo.ID, "SystemAdmin"));
                string str3 = sqlQuery;
                sqlQuery = str3 + " AND A." + BaseWorkReportTable.FieldStaffID + " IN (" + str2 + ")";
            }
            sqlQuery = sqlQuery + " ORDER BY " + BaseWorkReportTable.FieldWorkDate + " DESC ";
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        private void SetEntity(SQLBuilder sqlBuilder, BaseWorkReportEntity workReportEntity)
        {
            sqlBuilder.SetValue(BaseWorkReportTable.FieldWorkDate, workReportEntity.WorkDate);
            sqlBuilder.SetValue(BaseWorkReportTable.FieldCategoryID, workReportEntity.CategoryID);
            sqlBuilder.SetValue(BaseWorkReportTable.FieldContent, workReportEntity.Content);
            sqlBuilder.SetValue(BaseWorkReportTable.FieldManHour, workReportEntity.ManHour);
            sqlBuilder.SetValue(BaseWorkReportTable.FieldProjectID, workReportEntity.ProjectID);
            sqlBuilder.SetValue(BaseWorkReportTable.FieldStaffID, workReportEntity.StaffID);
            sqlBuilder.SetValue(BaseWorkReportTable.FieldEnabled, workReportEntity.Enabled);
            sqlBuilder.SetValue(BaseWorkReportTable.FieldAuditStaffID, workReportEntity.AuditStaffID);
            sqlBuilder.SetValue(BaseWorkReportTable.FieldScore, workReportEntity.Score);
            sqlBuilder.SetValue(BaseWorkReportTable.FieldDescription, workReportEntity.Description);
        }

        public double SumManHour(string paramStaffID, DateTime paramDate)
        {
            double num = 0.0;
            string sqlQuery = "SELECT SUM( " + BaseWorkReportTable.FieldManHour + ")FROM " + BaseWorkReportTable.TableName + " WHERE " + BaseWorkReportTable.FieldWorkDate + "='" + paramDate.ToShortDateString() + "' AND " + BaseWorkReportTable.FieldStaffID + "='" + paramStaffID + "'";
            string str2 = base.DbHelper.ExecuteScalar(sqlQuery).ToString();
            if (!string.IsNullOrEmpty(str2))
            {
                num = Convert.ToDouble(str2);
            }
            return num;
        }

        public int Update(BaseWorkReportEntity workReportEntity, out string statusCode)
        {
            int num = 0;
            if (DbLogic.IsModifed(base.DbHelper, BaseWorkReportTable.TableName, workReportEntity.ID, workReportEntity.ModifyUserID, workReportEntity.ModifyDate))
            {
                statusCode = StatusCode.ErrorChanged.ToString();
                return num;
            }
            num = this.UpdateEntity(workReportEntity);
            if (num == 1)
            {
                statusCode = StatusCode.OKUpdate.ToString();
                return num;
            }
            statusCode = StatusCode.ErrorDeleted.ToString();
            return num;
        }

        public int UpdateEntity(BaseWorkReportEntity workReportEntity)
        {
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginUpdate(BaseWorkReportTable.TableName);
            this.SetEntity(sqlBuilder, workReportEntity);
            sqlBuilder.SetValue(BaseWorkReportTable.FieldModifyUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseWorkReportTable.FieldModifyDate);
            sqlBuilder.SetWhere(BaseWorkReportTable.FieldID, workReportEntity.ID);
            return sqlBuilder.EndUpdate();
        }
    }
}